import * as dutyNotesService from '@/services/dutyNotesNew';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'dutyNotesNew',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id',

    callSelectData: null,
    callTypeData: null,
    callStatementData: null,
  },

  subscriptions: {},

  effects: {
    *queryData({ payload }, { call, put }) {
      const { callback, current, pageSize, ...extra } = payload;
      const response = yield call(
        dutyNotesService.queryData,
        { pageNo: current, pageSize },
        { ...extra },
      );
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: content,
            current,
            pageSize,
            total: typeof total === 'string' ? parseInt(total) : total,
          },
        });
      }
    },

    // 查询
    *queryData2({ payload }, { call, put }) {
      const { ...extra } = payload;
      const { current, pageSize, ...params } = extra;
      const response = yield call(
        dutyNotesService.queryData,
        { pageNo: current, pageSize },
        { ...params },
      );
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: content,
            current,
            pageSize,
            total: typeof total === 'string' ? parseInt(total) : total,
          },
        });
      }
    },

    // 删除
    *deleteData({ payload }, { call, put }) {
      try {
        const { list, ...extra } = payload;
        const option = {
          method: 'POST',
          body: list,
        };
        const res = yield call(dutyNotesService.deleteData, option);
        if (res) {
          message.success('删除成功');
        }
        // 刷新列表
        yield put({ type: 'queryData2', payload: { ...extra } });
      } catch (err) {
        message.error('删除失败');
        console.log('err', err);
      }
    },

    // 值班领导 数组
    *preview({ payload }, { call, put }) {
      try {
        const { unitId } = payload;
        const option = {
          method: 'POST',
          body: { unitId },
        };
        const response = yield call(dutyNotesService.preview, option);
        if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
          const { result = [] } = response;

          yield put({
            type: 'changeState',
            payload: {
              leaderList: result,
            },
          });
        }

      } catch (err) {
        console.log('err', err);
      }
    },

    //催告
    *noticeData({ payload }, { call, put }) {
      try {
        const { body, noticeType } = payload;
        const option = {
          method: 'POST',
          body,
        };
        let result = null;
        if (noticeType == 0) {
          result = yield call(dutyNotesService.noticeRollCall, option);
        }

        if (noticeType == 1) {
          result = yield call(dutyNotesService.noticeData, option);
        }
        message.success(result.result);
      } catch (err) {
        message.error('催告失败');
        console.log('err', err);
      }
    },
    // 话机点名select
    *callSelect({ payload }, { call, put }) {
      const response = yield call(dutyNotesService.callSelect, { method: 'POST' });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        yield put({
          type: 'changeState',
          payload: {
            callSelectData: result,
          },
        });

        yield put({
          type: 'callType',
          payload: {
            scene: result[0].scene,
          },
        });

        yield put({
          type: 'callStatement',
          payload: {
            sheechArt: result[0].scene,
          },
        });
      }
    },
    // 话机点名type
    *callType({ payload }, { call, put }) {
      const { scene } = payload;
      const option = {
        method: 'POST',
        body: { scene },
      };
      const response = yield call(dutyNotesService.callType, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        yield put({
          type: 'changeState',
          payload: {
            callTypeData: result,
          },
        });
      }
    },
    // 话机点名话术
    *callStatement({ payload }, { call, put }) {
      const { sheechArt } = payload;
      const option = {
        method: 'POST',
        body: { sheechArt },
      };
      const response = yield call(dutyNotesService.callStatement, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        yield put({
          type: 'changeState',
          payload: {
            callStatementData: result,
          },
        });
      }
    },

    // 编辑或添加
    *saveOrUpdateData({ payload }, { call, put, select }) {
      const file = yield select(state => state.dutyNotesNew.file);
      const { data, callback = () => { }, ...extra } = payload;
      let response = {};

      const formData = new FormData();
      formData.append('file', file);

      if (file && file.uid !== -1) {
        response = yield call(dutyNotesService.uploadfile, { method: 'POST', body: formData });
        if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
          const { result = {} } = response;
          data.originFileName = result.originFileName;
          data.transferFileName = result.transferFileName;
        }
      }

      const option = {
        method: 'POST',
        body: [data],
      };

      if (!data.id) {
        response = yield call(dutyNotesService.saveData, option);
      } else {
        response = yield call(dutyNotesService.updateData, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {

        message.success(!data.id ? '新增突发事件成功' : '编辑突发事件成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {}, file: undefined },
        });
        yield put({ type: 'queryData2', payload: { ...extra } });
      } else {
        message.error(!data.id ? '新增突发事件失败' : '编辑突发事件失败', 1);
      }
      callback();
    },

    // 下载
    *downloadData({ payload }, { call, put }) {
      try {
        const { callback, startTime, endTime, type } = payload;

        const response = yield call(dutyNotesService.downloadData, endTime, startTime, type);
        if (response) {
          console.log(typeof window.navigator.msSaveBlob);
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(response, name);
          } else {
            const URL = window.URL;
            const downloadUrl = URL.createObjectURL(response);
            const a = document.createElement('a');

            if (typeof a.download === 'undefined') {
              window.location = downloadUrl;
            } else {
              // a.download = '长宁区各部门事件上报表.xlsx';
              a.download = '值守保障信息汇报.docx';
              a.href = downloadUrl;
              a.style.display = 'none';
              document.body.appendChild(a);
              a.click();
              window.URL.revokeObjectURL(downloadUrl);
              document.body.removeChild(a);
            }
          }
          if (typeof callback === 'function') {
            callback();
          }
        } else {
          message.error('下载失败！');
        }
      } catch (e) {
        console.error(e);
      }
    },

    // 值班抽查单位 查询
    *spotlistDuty({ payload }, { call, put }) {
      const { callback = () => { } } = payload;
      const response = yield call(dutyNotesService.spotlistDuty, { method: 'POST' });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        yield put({
          type: 'changeState',
          payload: {
            spotCheckUnit: result,
          },
        });
        callback(result);
      }

    },

    // 值班抽查话术 查询
    *spotwordDuty({ payload }, { call, put }) {
      const { callback = () => { } } = payload;
      const response = yield call(dutyNotesService.spotwordDuty, { method: 'POST' });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        yield put({
          type: 'changeState',
          payload: {
            spotCheckHuashu: result,
          },
        });
        // callback(result);
      }
    },

    // 值班抽查、 语音催告
    *spotcheck({ payload }, { call, put }) {
      try {
        const { unitArr } = payload;
        const option = {
          method: 'POST',
          body: { unitArr },
        };
        const response = yield call(dutyNotesService.spotcheck, option);
        if (response && response.status == 200) {
          message.success('发送成功');
        } else {
          message.error('发送失败');
        }

      } catch (err) {
        console.log('err', err);
      }
    },

    // 值守保障 上传附件
    *uploadfile({ payload }, { call, put }) {
      const { callback, file } = payload;

      const formData = new FormData();
      formData.append('file', file);

      const response = yield call(dutyNotesService.uploadfile, { method: 'POST', body: formData });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        yield put({
          type: 'changeState',
          payload: {
            originFileName: result.originFileName,
            transferFileName: result.transferFileName,
          },
        });
        if (typeof callback === 'function') {
          callback(result);
        }
      }
    },

    // 值守保障 下载附件
    *downloadfile({ payload }, { call, put, select }) {
      try {
        const { eventallId, fileName } = payload;
        const response = yield call(dutyNotesService.downloadfile, eventallId);
        if (response) {
          const downloadUrl = URL.createObjectURL(response);
          const a = document.createElement('a');
          a.download = fileName;
          a.href = downloadUrl;
          a.style.display = 'none';
          document.body.appendChild(a);
          a.click();
          window.URL.revokeObjectURL(downloadUrl);
          document.body.removeChild(a);
        } else {
          message.error('下载失败！');
        }
      } catch (e) {
        console.error(e);
      }
    },

  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id',
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
